package com.newsanalysis.service.impl;

import com.newsanalysis.model.entity.News;
import com.newsanalysis.repository.NewsRepository;
import com.newsanalysis.service.NewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class NewsServiceImpl implements NewsService {
    
    @Autowired
    private NewsRepository newsRepository;
    
    @Override
    @Transactional
    public News save(News news) {
        return newsRepository.save(news);
    }
    
    @Override
    public Optional<News> findById(Long id) {
        return newsRepository.findById(id);
    }
    
    @Override
    public Page<News> findAll(Pageable pageable) {
        if (pageable == null) {
            pageable = Pageable.unpaged();
        }
        return newsRepository.findAll(pageable);
    }
    
    public List<News> findAllAsList() {
        return newsRepository.findAll();
    }
    
    @Override
    public Page<News> findByCategory(String category, Pageable pageable) {
        return newsRepository.findByCategory(category, pageable);
    }
    
    @Override
    public Page<News> searchByKeyword(String keyword, Pageable pageable) {
        return newsRepository.searchByKeyword(keyword, pageable);
    }
    
    @Override
    @Transactional
    public boolean updateNews(News news) {
        if (!newsRepository.existsById(news.getId())) {
            return false;
        }
        
        newsRepository.save(news);
        return true;
    }
    
    @Override
    @Transactional
    public boolean deleteById(Long id) {
        if (!newsRepository.existsById(id)) {
            return false;
        }
        
        newsRepository.deleteById(id);
        return true;
    }
    
    @Override
    @Transactional
    public int batchSave(List<News> newsList) {
        List<News> savedNewsList = newsRepository.saveAll(newsList);
        return savedNewsList.size();
    }
    
    @Override
    public List<News> findTopHotNews(int limit) {
        return newsRepository.findTop10ByOrderByHotIndexDesc()
                .stream()
                .limit(limit)
                .collect(Collectors.toList());
    }
    
    @Override
    public Map<String, Long> getCategoryDistribution() {
        Map<String, Long> distribution = new HashMap<>();
        List<Object[]> results = newsRepository.countByCategory();
        
        for (Object[] result : results) {
            String category = (String) result[0];
            Long count = (Long) result[1];
            
            if (category == null) {
                distribution.put("未分类", count);
            } else {
                distribution.put(category, count);
            }
        }
        
        return distribution;
    }
    
    @Override
    public Map<String, Long> getSentimentDistribution() {
        Map<String, Long> distribution = new HashMap<>();
        List<Object[]> results = newsRepository.countBySentiment();
        
        for (Object[] result : results) {
            String sentiment = (String) result[0];
            Long count = (Long) result[1];
            
            if (sentiment == null) {
                distribution.put("未分析", count);
            } else {
                distribution.put(sentiment, count);
            }
        }
        
        return distribution;
    }
    
    public List<News> findAllNews() {
        return newsRepository.findAll();
    }
}

